package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.component.DataLabelsComponent;
import com.zygh.policeasset.mapper.AssetsMapper;
import com.zygh.policeasset.mapper.AuditFlowMapper;
import com.zygh.policeasset.mapper.DailyProjectMapper;
import com.zygh.policeasset.mapper.UploadFileMapper;
import com.zygh.policeasset.pojo.DailyProject;
import com.zygh.policeasset.pojo.TableType;
import com.zygh.policeasset.pojo.UploadFile;
import com.zygh.policeasset.utils.AuditUtil;
import com.zygh.policeasset.utils.EasyExcelUtil;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/DailyProjectService.class */
public class DailyProjectService {
    @Autowired
    DailyProjectMapper dailyProjectMapper;
    @Autowired
    UploadFileMapper uploadFileMapper;
    @Autowired
    EasyExcelUtil easyExcelUtil;
    @Autowired
    AuditFlowMapper auditFlowMapper;
    @Autowired
    AuditFlowService auditFlowService;
    @Autowired
    AssetsMapper assetsMapper;

    public int add(DailyProject dailyAsset) {
        String now = DateUtil.now();
        String flowNo = AuditUtil.getFlowNo(now);
        dailyAsset.setFlowNo(flowNo);
        dailyAsset.setDataLabelsId(DataLabelsComponent.toEnableId);
        this.dailyProjectMapper.add(dailyAsset);
        this.auditFlowService.add(dailyAsset.getUserId(), flowNo, now, TableType.DAILYPROJECT.getName(), TableType.DAILYPROJECT.getType(), String.valueOf(dailyAsset.getId()), null);
        List<UploadFile> uploadFileList = dailyAsset.getUploadFileList();
        if (!StringUtils.isEmpty(uploadFileList)) {
            for (int i = 0; i < uploadFileList.size(); i++) {
                uploadFileList.get(i).setProjectId(dailyAsset.getId().intValue());
                uploadFileList.get(i).setProjectName("daily");
                uploadFileList.get(i).setCreateDate(DateUtil.now());
                this.uploadFileMapper.add(uploadFileList.get(i));
            }
        }
        List<UploadFile> invoiceFileList = dailyAsset.getInvoiceFileList();
        if (!StringUtils.isEmpty(invoiceFileList)) {
            for (int i = 0; i < invoiceFileList.size(); i++) {
                invoiceFileList.get(i).setProjectId(dailyAsset.getId().intValue());
                invoiceFileList.get(i).setProjectName("daily");
                invoiceFileList.get(i).setCreateDate(DateUtil.now());
                invoiceFileList.get(i).setFileType(1);
                this.uploadFileMapper.add(invoiceFileList.get(i));
            }
        }

        return dailyAsset.getId().intValue();
    }

    public PageInfo findAll(int pageNum, int pagesize, Integer assetNewStatus, String company, String category, String projectName, Integer dataLabelsId, Integer userId, Integer status, Double combined) {
        PageHelper.startPage(pageNum, pagesize);
        return new PageInfo(tolist(this.dailyProjectMapper.findAll(assetNewStatus, company, category, projectName, dataLabelsId, userId, status, combined)));
    }

    public List<DailyProject> tolist(List<DailyProject> list) {
        for (DailyProject dailyProject : list) {
            if ("zn".equals(dailyProject.getCategory())) {
                dailyProject.setCategoryName("智能");
            } else if ("jc".equals(dailyProject.getCategory())) {
                dailyProject.setCategoryName("基础");
            }
        }
        return list;
    }

    public String findallExcel(Integer assetNewStatus, String constructionuUnit, String category, String projectName, Integer dataLabelsId, HttpServletResponse response, Integer userId, Integer status, Double combined) {
        return this.easyExcelUtil.findallExcel(response, this.dailyProjectMapper.findAll(assetNewStatus, constructionuUnit, category, projectName, dataLabelsId, userId, status, combined));
    }

    public DailyProject findById(Integer id) {
        DailyProject dailyProject = this.dailyProjectMapper.findById(id);
        if (dailyProject.getCategory().equals("zn")) {
            dailyProject.setCategoryName("智能");
        } else {
            dailyProject.setCategoryName("基础");
        }
        return dailyProject;
    }

    public int update(DailyProject dailyAsset) {
        return this.dailyProjectMapper.update(dailyAsset).intValue();
    }

    @Transactional(rollbackFor = {Exception.class})
    public int deleteById(Integer id) {
        try {
            this.uploadFileMapper.delete(id.intValue(), "daily");
            return this.dailyProjectMapper.delete(id).intValue();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
    }

    public int updateByid(Integer assetNewStatus, Integer id) {
        return this.dailyProjectMapper.updateByid(assetNewStatus, id).intValue();
    }

    public String findTypeById(Integer id) {
        return this.dailyProjectMapper.findTypeById(id);
    }

    public Integer findAssetsAppCount(Integer userId) {
        return this.dailyProjectMapper.findAssetsAppCount(userId);
    }

    public int updateSPD(String startProvisionDate, String certificateNum, Integer id) {
        return this.dailyProjectMapper.updateSPD(startProvisionDate, certificateNum, id).intValue();
    }

    public Integer selectByName(String projectName) {
        return this.dailyProjectMapper.selectByName(projectName);
    }
}